package com.chance.cc.crawler.development.command.schedule.taobao;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.development.command.meta.oauth.CrawlerOauthCommand;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bx
 * @date 2020/12/21 0021 16:45
 */
@Data
@AllArgsConstructor
public class TaobaoUserAuthInfo {

    String category;
    String account;
    String cookie;

    public static void main(String[] args) throws IOException {
        List<String> kwLines = IOUtils.readLines(new FileInputStream("D:\\chance\\gitspace\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\schedule\\taobao\\taobao_oauth_info.txt"), Charset.forName("UTF-8"));

        List<Map<String,String>> allUserOauthInfos = new ArrayList<>();
        for (String kwLine : kwLines) {
            if (kwLine.startsWith("#")){
                continue;
            }
            String[] tbKeyword = kwLine.split("\t");
            Map<String,String> cookieInfos = new HashMap<>();
            cookieInfos.put("category",tbKeyword[0]);
            cookieInfos.put("account",tbKeyword[1]);
            cookieInfos.put("cookie",tbKeyword[2]);

            Map<String,String> userOauthInfoMap = new HashMap<>();
            userOauthInfoMap.put("domain","taobao");
            userOauthInfoMap.put("oauthInfo",JSON.toJSONString(cookieInfos));

            allUserOauthInfos.add(userOauthInfoMap);
        }

        CrawlerOauthCommand crawlerOauthCommand = new CrawlerOauthCommand("192.168.1.215",9599);


        HttpPage httpPage = crawlerOauthCommand.addOrUpdateUserOAuthInfo(allUserOauthInfos);

        System.out.println(httpPage.getRawText());
    }
}
